package com.atguigu.java8.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestLambda3 {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, Employee.Status.FREE),
            new Employee("李四", 38, 5555.99, Employee.Status.BUSY),
            new Employee("王五", 50, 6666.66, Employee.Status.VOCATION),
            new Employee("赵六", 16, 3333.33, Employee.Status.FREE),
            new Employee("田七", 8, 7777.77, Employee.Status.BUSY)
    );

    @Test
    public void test1() {
        Collections.sort(employees, (e1, e2) -> {
            if (e1.getAge() == e2.getAge()) {
                return e1.getName().compareTo(e2.getName());
            } else {
                // return -Integer.compare(e1.getAge(), e2.getAge());
                return Integer.compare(e1.getAge(), e2.getAge());
            }
        });

        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    // 需求：用于处理字符串
    @Test
    public void test2() {
        String result = strHandle("\t\t\t Lambda Expression", str -> str.trim());
        System.out.println(result);

        result = strHandle("lambda", str -> str.toUpperCase());
        System.out.println(result);

        result = strHandle("lambda", str -> str.substring(1, 3));
        System.out.println(result);
    }

    private String strHandle(String value, MyFunction myFunction) {
        return myFunction.getValue(value);
    }

    // 需求：对两个long型数据进行处理
    @Test
    public void test3() {
        op(100L, 200L, (x, y) -> x + y);
        op(100L, 200L, (x, y) -> x * y);
    }

    private void op(Long l1, Long l2, MyFunction2<Long, Long> myFunction2) {
        System.out.println(myFunction2.getValue(l1, l2));
    }

}
